﻿using System.Diagnostics;
using System.Windows.Shapes;

namespace KinonekoSoftware.Studio.Pages.Instances.Personalities
{
    public partial class FourQuadrantDiagramView : UserControl
    {
        public static readonly  DependencyProperty XProperty;
        public static readonly  DependencyProperty YProperty;
        public static readonly  DependencyProperty FirstQuadrantProperty;
        public static readonly  DependencyProperty SecondQuadrantProperty;
        public static readonly  DependencyProperty ThirdQuadrantProperty;
        public static readonly  DependencyProperty FourthQuadrantProperty;
        public static readonly  DependencyProperty IntroOfFirstQuadrantProperty;
        public static readonly  DependencyProperty ColorOfFirstQuadrantProperty;
        public static readonly  DependencyProperty IntroOfSecondQuadrantProperty;
        public static readonly  DependencyProperty ColorOfSecondQuadrantProperty;
        public static readonly  DependencyProperty IntroOfThirdQuadrantProperty;
        public static readonly  DependencyProperty ColorOfThirdQuadrantProperty;
        public static readonly  DependencyProperty IntroOfFourthQuadrantProperty;
        public static readonly  DependencyProperty ColorOfFourthQuadrantProperty;
        private static readonly SolidColorBrush    Common = new SolidColorBrush(Colors.Maroon);

        static FourQuadrantDiagramView()
        {
            XProperty = DependencyProperty.Register(nameof(X),
                                                    typeof(int),
                                                    typeof(FourQuadrantDiagramView),
                                                    new PropertyMetadata(3, OnXChanged));

            YProperty = DependencyProperty.Register(nameof(Y),
                                                    typeof(int),
                                                    typeof(FourQuadrantDiagramView),
                                                    new PropertyMetadata(3, OnYChanged));

            FirstQuadrantProperty = DependencyProperty.Register(nameof(FirstQuadrant),
                                                                typeof(string),
                                                                typeof(FourQuadrantDiagramView),
                                                                new PropertyMetadata(default(string)));

            SecondQuadrantProperty = DependencyProperty.Register(nameof(SecondQuadrant),
                                                                 typeof(string),
                                                                 typeof(FourQuadrantDiagramView),
                                                                 new PropertyMetadata(default(string)));

            ThirdQuadrantProperty = DependencyProperty.Register(nameof(ThirdQuadrant),
                                                                typeof(string),
                                                                typeof(FourQuadrantDiagramView),
                                                                new PropertyMetadata(default(string)));

            FourthQuadrantProperty = DependencyProperty.Register(nameof(FourthQuadrant),
                                                                 typeof(string),
                                                                 typeof(FourQuadrantDiagramView),
                                                                 new PropertyMetadata(default(string)));

            IntroOfFirstQuadrantProperty = DependencyProperty.Register(nameof(IntroOfFirstQuadrant),
                                                                       typeof(string),
                                                                       typeof(FourQuadrantDiagramView),
                                                                       new PropertyMetadata(default(string)));

            IntroOfSecondQuadrantProperty = DependencyProperty.Register(nameof(IntroOfSecondQuadrant),
                                                                        typeof(string),
                                                                        typeof(FourQuadrantDiagramView),
                                                                        new PropertyMetadata(default(string)));

            IntroOfThirdQuadrantProperty = DependencyProperty.Register(nameof(IntroOfThirdQuadrant),
                                                                       typeof(string),
                                                                       typeof(FourQuadrantDiagramView),
                                                                       new PropertyMetadata(default(string)));

            IntroOfFourthQuadrantProperty = DependencyProperty.Register(nameof(IntroOfFourthQuadrant),
                                                                        typeof(string),
                                                                        typeof(FourQuadrantDiagramView),
                                                                        new PropertyMetadata(default(string)));

            ColorOfFirstQuadrantProperty = DependencyProperty.Register(nameof(ColorOfFirstQuadrant),
                                                                       typeof(SolidColorBrush),
                                                                       typeof(FourQuadrantDiagramView),
                                                                       new FrameworkPropertyMetadata(new SolidColorBrush(Color.FromRgb(0xfe, 0xb1, 0xca)), FrameworkPropertyMetadataOptions.AffectsRender));

            ColorOfSecondQuadrantProperty = DependencyProperty.Register(nameof(ColorOfSecondQuadrant),
                                                                        typeof(SolidColorBrush),
                                                                        typeof(FourQuadrantDiagramView),
                                                                        new FrameworkPropertyMetadata(new SolidColorBrush(Color.FromRgb(0xf3, 0xe2, 0xb3)), FrameworkPropertyMetadataOptions.AffectsRender));

            ColorOfThirdQuadrantProperty = DependencyProperty.Register(nameof(ColorOfThirdQuadrant),
                                                                       typeof(SolidColorBrush),
                                                                       typeof(FourQuadrantDiagramView),
                                                                       new FrameworkPropertyMetadata(new SolidColorBrush(Color.FromRgb(0x91, 0xbe, 0xd7)), FrameworkPropertyMetadataOptions.AffectsRender));

            ColorOfFourthQuadrantProperty = DependencyProperty.Register(nameof(ColorOfFourthQuadrant),
                                                                        typeof(SolidColorBrush),
                                                                        typeof(FourQuadrantDiagramView),
                                                                        new FrameworkPropertyMetadata(new SolidColorBrush(Color.FromRgb(0xc3, 0xbf, 0xe4)), FrameworkPropertyMetadataOptions.AffectsRender));
        }

        public FourQuadrantDiagramView()
        {
            InitializeComponent();
        }

        private static void OnXChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var sender = (FourQuadrantDiagramView)d;
            var val    = (int)e.NewValue;

            Canvas.SetLeft(sender.Thumb, (val * 16) + 172);
        }

        private static void OnYChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var sender = (FourQuadrantDiagramView)d;
            var val    = (int)e.NewValue;

            Canvas.SetTop(sender.Thumb, (val * 16) + 172);
        }

        private void PerformPointerPressed(Point point)
        {
            var x = (int)Math.Round(point.X);
            var y = (int)Math.Round(point.Y);

            var x1 = (int)Math.Clamp(point.X, 20, 340);
            var y1 = (int)Math.Clamp(point.Y, 20, 340);
            var aw = (x1 - 20) / 16 - 10;
            var ah = (y1 - 20) / 16 - 10;

            X = aw;
            Y = ah;

            Canvas.SetLeft(Thumb,x - 8);
            Canvas.SetTop(Thumb, y- 8);
            Debug.WriteLine(point);
        }



        private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var pos = e.GetPosition(sender as IInputElement);
            PerformPointerPressed(pos);
            base.OnMouseLeftButtonDown(e);
        }

        private void Canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseOver && e.LeftButton == MouseButtonState.Pressed)
            {
                var pos = e.GetPosition(sender as IInputElement);
                PerformPointerPressed(pos);
            }
        }


        public SolidColorBrush ColorOfFourthQuadrant
        {
            get => (SolidColorBrush)GetValue(ColorOfFourthQuadrantProperty);
            set => SetValue(ColorOfFourthQuadrantProperty, value);
        }

        public string IntroOfFourthQuadrant
        {
            get => (string)GetValue(IntroOfFourthQuadrantProperty);
            set => SetValue(IntroOfFourthQuadrantProperty, value);
        }

        public string FourthQuadrant
        {
            get => (string)GetValue(FourthQuadrantProperty);
            set => SetValue(FourthQuadrantProperty, value);
        }

        public SolidColorBrush ColorOfThirdQuadrant
        {
            get => (SolidColorBrush)GetValue(ColorOfThirdQuadrantProperty);
            set => SetValue(ColorOfThirdQuadrantProperty, value);
        }

        public string IntroOfThirdQuadrant
        {
            get => (string)GetValue(IntroOfThirdQuadrantProperty);
            set => SetValue(IntroOfThirdQuadrantProperty, value);
        }

        public string ThirdQuadrant
        {
            get => (string)GetValue(ThirdQuadrantProperty);
            set => SetValue(ThirdQuadrantProperty, value);
        }

        public SolidColorBrush ColorOfSecondQuadrant
        {
            get => (SolidColorBrush)GetValue(ColorOfSecondQuadrantProperty);
            set => SetValue(ColorOfSecondQuadrantProperty, value);
        }

        public SolidColorBrush IntroOfSecondQuadrant
        {
            get => (SolidColorBrush)GetValue(IntroOfSecondQuadrantProperty);
            set => SetValue(IntroOfSecondQuadrantProperty, value);
        }

        public SolidColorBrush SecondQuadrant
        {
            get => (SolidColorBrush)GetValue(SecondQuadrantProperty);
            set => SetValue(SecondQuadrantProperty, value);
        }

        public SolidColorBrush ColorOfFirstQuadrant
        {
            get => (SolidColorBrush)GetValue(ColorOfFirstQuadrantProperty);
            set => SetValue(ColorOfFirstQuadrantProperty, value);
        }

        public string IntroOfFirstQuadrant
        {
            get => (string)GetValue(IntroOfFirstQuadrantProperty);
            set => SetValue(IntroOfFirstQuadrantProperty, value);
        }

        public string FirstQuadrant
        {
            get => (string)GetValue(FirstQuadrantProperty);
            set => SetValue(FirstQuadrantProperty, value);
        }

        public int Y
        {
            get => (int)GetValue(YProperty);
            set => SetValue(YProperty, value);
        }

        public int X
        {
            get => (int)GetValue(XProperty);
            set => SetValue(XProperty, value);
        }
    }
}